<template>
  <div>
    <div class="container" v-if="!showcheckreport">
      <div class="container-search">
        <!-- <div class="container-search-top"></div> -->
        <div class="container-search-bottom">
          <!-- 筛选条件 -->
          <div class="search">
            <div class="search-item">
              <el-input
                v-model="params.name"
                placeholder="请输入单位名称"
                clearable
                maxlength="25"
                class="search-input"
              />
            </div>
            <div class="search-item">
              <el-cascader
                v-model="params.region"
                placeholder="请选择行政区划"
                :options="options"
                filterable
                clearable
                @change="handlerregion"
                :props="areaprops"
              />
            </div>
            <div class="search-item">
              <el-select
                v-model="params.typeId"
                placeholder="请选择单位类型"
                clearable
                class="search-input"
              >
                <el-option
                  v-for="dict in unittypeList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </div>
            <div class="search-item">
              <el-select
                v-model="params.levelId"
                placeholder="请选择单位级别"
                clearable
                class="search-input"
              >
                <el-option
                  v-for="dict in unitlevelList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </div>
            <div class="search-item">
              <el-select
                v-model="params.industryId"
                placeholder="请选择所属行业"
                clearable
                class="search-input"
              >
                <el-option
                  v-for="dict in supervisorList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </div>
            <div class="search-item">
              <el-button type="primary" @click="getcheckList"
                ><img src="@/assets/images/search.png" alt="" /> 搜索</el-button
              >
              <el-button color="#eff3ff" :dark="true" @click="reset"
                ><img src="@/assets/images/reset.png" alt="" />
                <span style="color: #6d98fd">重置 </span>
              </el-button>
            </div>
            <div class="search-item">
              <el-select
                v-model="params.taskSource"
                placeholder="请选择任务来源"
                clearable
                class="search-input"
              >
                <el-option
                  v-for="dict in sourceList"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </div>
            <div class="search-item">
              <el-date-picker
                v-model="params.dateRange"
                type="daterange"
                align="right"
                range-separator="至"
                start-placeholder="检查开始日期"
                end-placeholder="检查结束日期"
                format="YYYY/MM/DD"
                value-format="YYYY-MM-DD"
              />
            </div>
          </div>
        </div>
      </div>

      <div class="container-table">
        <!-- 表格筛选 -->
        <div class="filter">
          <div
            class="filter-item"
            :class="activeindex === '' ? 'active' : ''"
            @click="handlerfilter('')"
          >
            <img
              src="@/assets/images/all.png"
              alt=""
              srcset=""
              v-if="activeindex === ''"
            />
            <img src="@/assets/images/blueall.png" alt="" srcset="" v-else />
            <div class="item-text">全部 （{{ allData || 0 }}）</div>
          </div>
          <div
            class="filter-item"
            :class="activeindex == 1 ? 'active' : ''"
            @click="handlerfilter(1)"
          >
            <img
              src="@/assets/images/waitcheck.png"
              alt=""
              srcset=""
              v-if="activeindex == 1"
            />
            <img
              src="@/assets/images/bluewaitcheck.png"
              alt=""
              srcset=""
              v-else
            />

            <div class="item-text">处置中 （{{ atDisposal || 0 }}）</div>
          </div>
          <div
            class="filter-item"
            :class="activeindex == 2 ? 'active' : ''"
            @click="handlerfilter(2)"
          >
            <img
              src="@/assets/images/check.png"
              alt=""
              srcset=""
              v-if="activeindex == 2"
            />
            <img src="@/assets/images/bluecheck.png" alt="" srcset="" v-else />

            <div class="item-text">已处置 （{{ disposed || 0 }}）</div>
          </div>
          <div
            class="filter-item"
            :class="activeindex === 0 ? 'active' : ''"
            @click="handlerfilter(0)"
          >
            <img
              src="@/assets/images/check.png"
              alt=""
              srcset=""
              v-if="activeindex === 0"
            />
            <img src="@/assets/images/bluecheck.png" alt="" srcset="" v-else />

            <div class="item-text">无需处置 （{{ noDisposal || 0 }}）</div>
          </div>
        </div>
        <!-- 表格数据 -->
        <el-table
          v-loading="loading"
          :data="checkList"
          border
          height="52vh"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="操作"
            :show-overflow-tooltip="true"
            width="150"
            :resizable="false"
          >
            <template #default="{ row }">
              <div class="table-operate">
                <span class="detail" @click="handleOperate('detail', row)"
                  >详情</span
                >
                <span
                  class="edit"
                  @click="handleOperate('edit', row)"
                  v-if="
                    [1].includes(userStore.deptType) &&
                    (!row.followStatus || row.followStatus != 1)
                  "
                  >编辑</span
                >
              </div>
            </template>
          </el-table-column>
          <el-table-column
            label="单位名称"
            prop="name"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="行政区划"
            prop="areaName"
            width="300"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="单位类型"
            prop="institutionType"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="检查单位"
            prop="policeStationName"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="任务来源"
            prop="taskSourceName"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
          <el-table-column
            label="不达标/达标事项"
            prop="policeStationName"
            :show-overflow-tooltip="true"
          >
            <template #default="{ row }">
              <span style="color: #e7350f">{{
                row.notStandard ? row.notStandard : 0
              }}</span
              >项/
              <span style="color: #2f9a78">{{
                row.standard ? row.standard : 0
              }}</span
              >项
            </template>
          </el-table-column>
          <el-table-column
            label="处置状态"
            prop="checkStatusName"
            :show-overflow-tooltip="true"
            width="100"
          >
          </el-table-column>
          <el-table-column
            label="检查时间"
            prop="checkDate"
            :show-overflow-tooltip="true"
          >
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <pagination
          class="pagination"
          :total="total"
          v-model:page="params.pageNum"
          v-model:limit="params.pageSize"
          @pagination="getcheckList"
        />
      </div>
    </div>
    <!-- 检查记录详情 -->
    <el-dialog
      v-model="showdialog"
      width="1000px"
      append-to-body
      class="dialogBox"
    >
      <template #header="{ close, titleId, titleClass }">
        <div class="my-header">
          <img src="@/assets/images/title-card.png" alt="" />
          <div class="header-title">检查记录详情</div>
        </div>
      </template>
      <Taskdtl ref="taskdetailref" pageType="check" v-if="showdialog"></Taskdtl>
    </el-dialog>

    <!-- 检查上报 -->
    <Checkreport
      ref="checkreportref"
      v-if="showcheckreport"
      v-model:showcheckreport="showcheckreport"
      @refreshList="reset"
    ></Checkreport>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import Card from "@/components/Card";
import Taskdtl from "../task/taskdtl";
import Checkreport from "../task/Checkreport";
import { getchecklist, getcheckStatusCount } from "@/api/check/check";
import { getDictsApi, getAreaList } from "@/api/dict";
import { getToken } from "@/utils/auth";
import { ElMessage, ElMessageBox } from "element-plus";
import useUserStore from "@/store/modules/user";

const userStore = useUserStore();
const { proxy } = getCurrentInstance();

// 搜索参数
let params = reactive({
  pageNum: 1,
  pageSize: 20,
  name: "",
  typeId: "",
  levelId: "",
  industryId: "",
  checkStatus: "",
  taskSource: "",
  region: [],
  dateRange: [],
  provinceId: "",
  cityId: "",
  zoneId: "",
  streetId: "",
});

// 总数
const total = ref(0);

//行政区划数据
let options = ref([]);

const areaprops = {
  lazy: true,
  checkStrictly: true,
  lazyLoad(node, resolve) {
    const { level } = node;
    let datas = [];
    setTimeout(() => {
      let code = 1;
      if (level != 0) {
        code = node.data.value;
      }
      getAreaList({ pid: code }).then((response) => {
        if (response.data && response.data.length) {
          datas = response.data.map((item, i) => ({
            value: item.value,
            label: item.label,
            leaf: level >= 3,
            // disabled: level == 0, // 这里可以设置为 true 来禁用这个选项
          }));
        } else {
          datas = [];
        }
        resolve(datas);
      });
    }, 200);
  },
};

// 表格加载效果
const loading = ref(false);
//检查列表
const checkList = ref([]);
//时间选择

// 单位类型下拉
const unittypeList = ref([]);
// 单位级别下拉
const unitlevelList = ref([]);
// 所属行业下拉
const supervisorList = ref([]);
//任务来源下拉
const sourceList = ref([]);
// 详情弹窗的显隐
let showdialog = ref(false);
let showcheckreport = ref(false);
//检查上报的引用
const checkreportref = ref(null);
//选中的项
let activeindex = ref("");

//详情组件的引用
const taskdetailref = ref(null);
//省市区校验规则
const addunitValidate = (rule, value, callback) => {
  if (normalData.value.length == 0) {
    callback(new Error("请添加单位"));
  } else {
    callback();
  }
};
const queryRules = ref({
  addunit: [
    {
      required: true,
      validator: addunitValidate,
      message: "请添加单位",
      trigger: "change",
    },
  ],
  lastDate: [{ required: true, message: "请选择截止时间", trigger: "change" }],
});
/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

/**
 * @description: 表格数据接口
 */
const getcheckList = () => {
  loading.value = true;
  if (params.dateRange) {
    if (params.dateRange.length > 0) {
      params.startDate = params.dateRange[0];
      params.endDate = params.dateRange[1];
    }
  } else {
    params.dateRange = [];
    params.startDate = "";
    params.endDate = "";
  }
  getchecklist(
    { pageNum: params.pageNum, pageSize: params.pageSize },
    params
  ).then((res) => {
    checkList.value = res.rows;
    total.value = res.total;
    loading.value = false;
    checkStatusCount();
  });
};

/**
 * @description: 状态数据
 */
let allData = ref(0);
let atDisposal = ref(0);
let disposed = ref(0);
let noDisposal = ref(0);
const checkStatusCount = () => {
  getcheckStatusCount(
    { pageNum: params.pageNum, pageSize: params.pageSize },
    params
  ).then((res) => {
    allData.value = res.data.allData;
    atDisposal.value = res.data.atDisposal;
    disposed.value = res.data.disposed;
    noDisposal.value = res.data.noDisposal;
  });
};

/**
 * @description: 重置
 */
const reset = () => {
  Object.assign(params, {
    pageNum: 1,
    pageSize: 20,
    name: "",
    typeId: "",
    levelId: "",
    industryId: "",
    taskSource: "",
    region: [],
    checkStatus: "",
    dateRange: [],
    endDate: "",
    startDate: "",
    provinceId: "",
    cityId: "",
    zoneId: "",
    streetId: "",
  });
  getcheckList();
  activeindex.value = "";
};

/**
 * @description: tab操作
 */
const handlerfilter = (index) => {
  activeindex.value = index;
  params.checkStatus = index;
  getcheckList();
};

/**
 * @description: 联级处理省市区操作
 * @param {String} type 类型
 */
const handlerregion = (value) => {
  if (value) {
    switch (value.length) {
      case 1:
        params.provinceId = value[0];
        break;
      case 2:
        params.provinceId = value[0];
        params.cityId = value[1];
        break;
      case 3:
        params.provinceId = value[0];
        params.cityId = value[1];
        params.zoneId = value[2];
        break;
      case 4:
        params.provinceId = value[0];
        params.cityId = value[1];
        params.zoneId = value[2];
        params.streetId = value[3];
        break;

      default:
        break;
    }
  } else {
    params.region = [];
    params.provinceId = null;
    params.cityId = null;
    params.zoneId = null;
    params.streetId = null;
  }
};
/**
 * @description: 重置为初始状态
 */
const resetall = () => {
  //导入分页数据
  ImporttableList.value = [];
  //正常的数据
  normalData.value = [];
  //重复的数据
  repeatData.value = [];
  //异常的数据
  abnormalData.value = [];
  importsuccess.value = false;
};
/**
 * @description: 表格操作
 * @param {String} type 类型
 * @param {Object} row  当前行数据
 */
const handleOperate = async (type, row) => {
  switch (type) {
    case "detail":
      showdialog.value = true;
      nextTick(() => {
        taskdetailref.value.handleDetailInfo(row);
      });
      break;
    case "edit":
      showcheckreport.value = true;
      nextTick(() => {
        checkreportref.value.handleEditReport(row);
      });
      break;

    default:
      break;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  await getDictsApi("institution_type").then((res) => {
    //单位类型下拉
    if (res.code === 200) {
      unittypeList.value = res.data;
    }
  });
  await getDictsApi("institution_industry_type").then((res) => {
    //行业主管下拉
    if (res.code === 200) {
      supervisorList.value = res.data;
    }
  });
  await getDictsApi("fkwz_institution_level_type").then((res) => {
    //单位级别下拉
    if (res.code === 200) {
      unitlevelList.value = res.data;
    }
  });
  await getDictsApi("task_source_type").then((res) => {
    //任务来源下拉
    if (res.code === 200) {
      sourceList.value = res.data;
    }
  });
  // await getAreaList({ pid: 0 }).then((res) => {
  //   options.value = res.data;
  // });
}

onMounted(() => {
  getcheckList();
  checkStatusCount();
  getDicts();
});
</script>

<style scoped lang="scss">
.container {
  position: relative;
  .container-search {
    margin-bottom: 12px;
    .container-search-bottom {
      background-color: #ffffff;
      border-radius: 10px;
      padding: 15px 15px 15px 15px;
    }
  }

  .container-button {
    position: absolute;
    top: 15px;
    right: 15px;
    img {
      margin-right: 6px;
      margin-top: 2px;
    }
  }
  .container-table {
    background-color: #ffffff;
    border-radius: 10px;
    padding: 15px 15px 15px 15px;
    .filter {
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 590px;
      background: #ffffff;
      box-shadow: 0px 1px 4px 0px rgba(51, 51, 51, 0.1);
      border-radius: 20px;
      font-size: 14px;
      padding: 2px;
      margin-bottom: 10px;
      .filter-item {
        cursor: pointer;
        display: flex;
        align-items: center;
        padding: 10px 20px;
        border-radius: 20px;
        color: #7eb7fe;
        img {
          height: 16px;
          margin-right: 2px;
        }
      }
      .active {
        background-color: #4aa1fe;
        color: #ffffff;
      }
    }
    .check-tab {
      display: flex;
      .tab-item {
        display: flex;
        .tab {
          padding: 4px 12px;
          margin-right: 10px;
        }
        .tab-time {
          width: 110px;
          padding: 4px 12px;
        }
      }
      .orange {
        color: #ef8e4d;
        background-color: #f7e3c3;
        border-radius: 5px;
      }
      .green {
        color: #60cba7;
        background-color: #bbeddc;
        border-radius: 5px;
      }
    }
  }
}

.task-form {
  padding: 0 30px;
  .addunit {
    width: 100%;
    display: flex;
    flex-direction: column;
    .guide {
      cursor: pointer;
      width: 100%;
      background: #cae1f2;
      height: 26px;
      line-height: 26px;
      margin-top: 15px;
      color: #42abfa;
      text-indent: 2em;
    }
  }
}

.Import {
  padding: 0 30px 30px 30px;
  .item {
    display: flex;
    font-size: 12px;
    margin-bottom: 20px;
    .text {
      margin-left: 10px;
      color: #797979;
    }
  }
  .item:nth-child(1) {
    .text {
      color: #58e0ad;
    }
  }

  :deep(.el-upload-dragger) {
    padding: 0;
    width: 300px;
    height: 186px;
    margin: 0 auto;
    background-color: #f7fdff;
    border-radius: 10px;
  }
}

.Import-table {
  padding: 0 30px 20px 30px;
  .table-text {
    font-size: 14px;
    margin-bottom: 15px;
  }
}
.ai-select {
  display: flex;
  height: 610px;
  transform: translateY(-10px);
  color: #404141;
  .select-left {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    width: 20%;
    padding: 30px 20px;
    background: #fafeff;
    box-shadow: 2px 2px 8px 0px rgba(0, 0, 0, 0.1);
    .item {
      .title {
        margin-bottom: 20px;
      }
    }

    .item-btn {
      display: flex;
      justify-content: center;
      margin-top: 30px;
    }
  }
  .select-middle {
    padding: 0 20px;
    width: 60%;
    .middle-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 20px 0;
    }
  }
  .select-right {
    width: 20%;
    background: #fafeff;
    box-shadow: -2px 2px 8px 0px rgba(0, 0, 0, 0.1);
    padding: 0px 20px;
    .right-top {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 20px 0;
    }
    .right-content {
      height: 460px;
      .content-item {
        display: flex;
        align-items: center;
        justify-content: space-between;
        border-bottom: 1px solid #f4f6f6;
        padding: 10px 0;
      }
    }
    .content-btn {
      display: flex;
      justify-content: center;
      margin-top: 30px;
    }
  }
}
</style>

<style>
.ImportdialogBox {
  .el-pagination {
    margin-top: 20px;
    justify-content: center;
  }
}
</style>
